package com.ol.edu.client.service.impl;


import com.ol.edu.client.entity.ResponseDTO;
import com.ol.edu.client.entity.User;
import com.ol.edu.client.mapper.UserDao;
import com.ol.edu.client.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户表(User)表服务实现类
 *
 * @author LaoSun
 * @since 2020-09-08 10:55:49
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 登陆
     *
     * @param phone    手机号
     * @param password 密码
     * @return
     */
    public User login(String phone, String password) {
        return userDao.login(phone, password);
    }

    /**
     * 检查手机号是否被注册
     *
     * @param phone 手机号
     * @return
     */
    public Integer checkPhone(String phone) {
        return userDao.checkPhone(phone);
    }

    /**
     * 注册用户
     *
     * @param phone    手机号
     * @param password 密码
     * @param nickname 昵称
     * @param headimg  头像
     * @return
     */
    public Integer register(String phone, String password, String nickname, String headimg) {
        if (StringUtils.isEmpty(nickname)) {
            nickname = phone;
        }
        return userDao.register(phone, password, nickname, headimg);
    }

    /**
     * 更新用户头像与昵称
     * @param user
     * @return
     */
    @Override
    public ResponseDTO updateUserInfo(User user) {
        Integer res = userDao.updateUserInfo(user);
        ResponseDTO<Object> responseDTO = new ResponseDTO<>();
        if (res == 1) {
            responseDTO.setMessage("修改成功");
            responseDTO.setStatus(200);
            responseDTO.setSuccess(true);
            return responseDTO;
        } else {
            responseDTO.setMessage("修改失败");
            responseDTO.setStatus(500);
            responseDTO.setSuccess(false);
            return responseDTO;
        }
    }

    /**
     * 更新密码
     * @param user
     * @return
     */
    @Override
    public ResponseDTO updatePassword(User user) {
        Integer res = userDao.updatePassword(user);
        ResponseDTO<Object> responseDTO = new ResponseDTO<>();
        if (res == 1) {
            responseDTO.setMessage("修改成功");
            responseDTO.setStatus(200);
            responseDTO.setSuccess(true);
            return responseDTO;
        } else {
            responseDTO.setMessage("修改失败");
            responseDTO.setStatus(500);
            responseDTO.setSuccess(false);
            return responseDTO;
        }
    }

}